import {BaseApi} from "./BaseApi.ts";
import {buildPriMsgBody, buildPrivateMessageBody} from "../types/PrivateMessage.ts";
import {buildSimpleUserInfo} from "../types/User.ts";

const libs = {__lib: "message", __act: "message",}

export class MessageApi extends BaseApi {
    list = (page?: number | undefined) => this.instance.get("nuke.php", {
        params: {
            ...libs,
            act: "list",
            __output: 11,
            page
        },
    }).then(res => buildPrivateMessageBody(res.data.data))

    read = (mid: number, page?: number | undefined) => this.instance.get("nuke.php", {
        params: {
            ...libs,
            act: "read",
            __output: 11,
            mid, page
        },
    }).then(res => buildPriMsgBody(res.data.data))

    // 发起私信会话（一般用户每天一次）
    new = (title: string, content: string, users: string[]) => this.postNuke({
        act: "new",
        subject: title,
        content,
        to: users.join(',')
    })

    // 回复私信会话
    reply = (mid: number, content: string, title: string = "") => this.postNuke({
        act: "reply",
        mid,
        subject: title,
        content
    })

    // 邀请用户加入私信会话
    add = (mid: number, users: string[]) => this.postNuke({
        act: "add",
        mid,
        to: users.join(',')
    })

    // 从私信会话踢出用户
    kick = (mid: number, userId: number) => this.postNuke({
        act: "leave_topic",
        mid,
        luid: userId
    })
    // 私信黑名单查询
    blockList = () => this.postNuke({
        act: "list_block",
    }).then(res => res.map(buildSimpleUserInfo))
    // 添加用户到私信黑名单
    blockAdd = (userId: number) => this.postNuke({
        act: "add_block", buids: userId
    })
    // 从私信黑名单中删除用户
    blockDel = (userId: number) => this.postNuke({
        act: "del_block", buids: userId
    })

    postNuke = (params: any) => this.instance.post("nuke.php", {
        ...params, __lib: "message", __act: "message",
        __output: 11,
    }).then(res => res.data.data)

}